<script setup>
/**
 * 文件描述如下：
 * 1. 二次确认弹出框
 * 2. msg是传入的信息 是一个对象 有 { title:string, content:string }
 * 3. confirm 传入的确认的方法
 * 4. freeClose是否自由关闭
 */
import { ref, defineProps, onBeforeUnmount, onMounted } from 'vue'
import Loading from '/src/components/loading.vue'

const data = defineProps({
  msg: Object, // 一个对象控制 ui 显示
  cancel: Function,
  confirm: Function, // 确认方法
  freeClose: {
    type: Boolean,
    default: true,
  },
})

let status = ref(false)

const confirm = () => {
  if (status.value) return
  status.value = true
  data.confirm()
}

const close = () => {
  if (status.value) return
  if (data.freeClose != false) data.cancel()
}

const close2 = () => {
  if (status.value) return
  data.cancel()
}
</script>

<template>
  <div class="confirmDialog-container" @click="close">
    <div class="main" @click="(e) => e.stopPropagation()">
      <div class="title">{{ data?.msg?.title ?? '提示' }}</div>
      <div class="content">
        <p>
          {{ data?.msg?.content.title ?? '暂无提示' }}
          <span> {{ data?.msg?.content.text ?? '' }}</span>
        </p>
        <p v-if="data?.msg?.content2">
          {{ data?.msg?.content2?.title ?? '暂无提示' }}
          <span> {{ data?.msg?.content2?.text ?? '' }}</span>
        </p>
      </div>
      <div class="floor">
        <div @click="close2">取消</div>
        <div @click="confirm">
          <!-- -->
          <Loading :type="3" v-if="status" />
          <p v-else>确认</p>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="less" scoped>
.confirmDialog-container {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 99999;
  width: 100%;
  height: 100%;
  background-color: rgba(240, 240, 240, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
}
.main {
  width: 400px;
  height: 200px;
  background-color: white;
  box-shadow: 1px 1px 10px rgba(0, 0, 0, 0.2);
  display: flex;
  flex-direction: column;
}

.title {
  font-size: 16px;
  font-weight: 700;
  height: 30px;
  line-height: 30px;
  width: 100%;
  color: rgba(64, 149, 229, 0.9);
  padding-left: 12px;
  box-shadow: 0px 1px 1px rgba(0, 0, 0, 0.2);
}

.content {
  flex: 1;
  font-size: 16px;
  font-weight: 500;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding-left: 12px;
  > p {
    margin-top: 6px;
    span {
      color: red;
    }
  }
}

.floor {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  height: 40px;
  padding-right: 20px;
  > div {
    width: 42px;
    height: 22px;
    background-color: rgba(64, 149, 229, 0.9);
    border-radius: 2px;
    display: flex;
    justify-content: center;
    align-items: center;
    margin-left: 20px;
    color: white;
    cursor: pointer;
  }
  > div:nth-child(1) {
    border: 1px solid black;
    background-color: transparent;
    color: black;
  }
}
</style>
